<?php

namespace App\Http\Controllers\Api;

use App\UserCoinLog;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class RankController extends Controller
{
    public function gamewin(Request $request)
    {
        $logs = Cache::remember('rank_game_win', 1, function () {
            return DB::table('user_coin_log')
                ->leftJoin('user', 'user_coin_log.user_id', '=', 'user.id')
                ->whereIn('bussiness_id', [2, 4, 5, 6])
                ->where('type', 1)
                ->where('coin', '>', 0)
                ->where('desc', '<>', '推荐奖励')
                ->orderBy('user_coin_log.created_at', 'DESC')
                ->select(['user.nickname', 'user.avatar', 'user_coin_log.coin', 'user_coin_log.bussiness_id'])
                ->take(100)
                ->get();
        });

        return $this->echoJson(1000, '', $logs);
    }

    private function echoJson($code = 0, $msg = '', $data = [])
    {
        header('Content-type: application/json');
        $result = [
            'code' => $code,
            'msg' => $msg,
            'data' => $data,
        ];
        $json = json_encode($result);

        // JSONP
        if (!empty($_GET['callback'])) {
            $json = $_GET['callback'] . "(" . $json . ")";
        }
        echo $json;
        exit(0);
    }
}
